package com.maxmlr.music.song.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxmlr.music.admin.service.UserService;
import com.maxmlr.music.common.base.BaseServiceImpl;
import com.maxmlr.music.common.exception.XmlrException;
import com.maxmlr.music.song.dao.SongMapper;
import com.maxmlr.music.song.entity.Song;
import com.maxmlr.music.song.param.SongInfoParam;
import com.maxmlr.music.song.service.SongService;
import com.maxmlr.music.song.service.transfer.SongTransfer;
import com.maxmlr.music.system.service.FilesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author max
 * @version 1.0
 * @ClassName SongServiceImpl
 * @Description 歌曲信息表  服务实现类
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-07
 */
@Slf4j
@Service
public class SongServiceImpl extends BaseServiceImpl<SongMapper, Song> implements SongService {

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private FilesService filesService;

    @Autowired
    private UserService userService;

    @Autowired
    private SongTransfer songTransfer;


    @Override
    public Page<SongInfoParam> listSong(Page<SongInfoParam> page, SongInfoParam inParam) {
        if (Objects.nonNull(inParam) && Objects.nonNull(inParam.getNation())) {
            if (inParam.getNation() < 2) {
                inParam.setNation(null);
            }
        }
        songMapper.selectListSong(page, inParam);
        filesService.getFilesByIds(page.getRecords(), SongInfoParam::getHeadImgId, SongInfoParam::setHeadImg);
        return page;
    }

    @Override
    public SongInfoParam getSongInfoParam(Integer id, Integer type) {
        Song song = getById(id);
        SongInfoParam param = songTransfer.po2SongInfoParam(song);
        //0：全， 1 mp3,2 mv,3两者都有,4都没
        boolean headImgFlag = false;
        boolean mp3Flag = false;
        boolean mvFlag = false;
        if (Objects.isNull(type) || 0 == type) {
            headImgFlag = true;
            mp3Flag = true;
            mvFlag = true;
        } else if (1 == type) {
            mp3Flag = true;
        } else if (2 == type) {
            mvFlag = true;
        } else if (3 == type) {
            mp3Flag = true;
            mvFlag = true;
        }

        if (headImgFlag) {
            filesService.getFileById(param, SongInfoParam::getHeadImgId, SongInfoParam::setHeadImg);
        }
        if (mp3Flag) {
            filesService.getFileById(param, SongInfoParam::getMp3Id, SongInfoParam::setMp3);
        }
        if (mvFlag) {
            filesService.getFileById(param, SongInfoParam::getMvId, SongInfoParam::setMv);
        }
        return param;
    }

    @Override
    public boolean saveParam(SongInfoParam param) {
        Song one = getOne(Wrappers.<Song>lambdaQuery()
                .eq(Song::getSongName, param.getSongName())
                .eq(Song::getSingerId, param.getSingerId()));
        if (Objects.nonNull(one)) {
            throw new XmlrException("歌曲名重复");
        }
        Song song = songTransfer.po2Song(param, userService.getAdminUser());
        return save(song);
    }

    @Override
    public boolean updateParamById(SongInfoParam param) {
        Song song = getById(param.getId());
        if (Objects.isNull(song)) {
            throw new XmlrException("歌曲信息不存在");
        }
        songTransfer.fullSong(song, param, userService.getAdminUser());
        return updateById(song);
    }

    @Override
    public Page<SongInfoParam> listSongParam(Page<Song> page, SongInfoParam inParam, Integer type) {
        if (Objects.nonNull(inParam) && Objects.nonNull(inParam.getNation())) {
            if (inParam.getNation() < 2) {
                inParam.setNation(null);
            }
        }
        Song song = songTransfer.po2Song(inParam);
        page(page, Wrappers.query(song));
        Page<SongInfoParam> paramPage = songTransfer.po2PageSongInfoParam(page);
        paramPage.setRecords(page.getRecords().stream().map(songTransfer::po2SongInfoParam).collect(Collectors.toList()));
        //0：全， 1 mp3,2 mv,3两者都有,4都没
        boolean headImgFlag = false;
        boolean mp3Flag = false;
        boolean mvFlag = false;
        if (Objects.isNull(type)) {
            headImgFlag = true;
            mp3Flag = true;
            mvFlag = true;
        } else if (1 == type) {
            mp3Flag = true;
        } else if (2 == type) {
            mvFlag = true;
        } else if (3 == type) {
            mp3Flag = true;
            mvFlag = true;
        }

        if (headImgFlag) {
            filesService.getFilesByIds(paramPage.getRecords(), SongInfoParam::getHeadImgId, SongInfoParam::setHeadImg);
        }
        if (mp3Flag) {
            filesService.getFilesByIds(paramPage.getRecords(), SongInfoParam::getMp3Id, SongInfoParam::setMp3);
        }
        if (mvFlag) {
            filesService.getFilesByIds(paramPage.getRecords(), SongInfoParam::getMvId, SongInfoParam::setMv);
        }
        return paramPage;
    }

    @Override
    public Object transferParam(Song song) {
        SongInfoParam param = songTransfer.po2SongInfoParam(song);
        filesService.getFileById(param, SongInfoParam::getHeadImgId, SongInfoParam::setHeadImg);
        return param;
    }

    @Override
    public List<SongInfoParam> transferParams(List<Song> list) {
        List<SongInfoParam> params = list.stream().map(songTransfer::po2SongInfoParam).collect(Collectors.toList());
        filesService.getFilesByIds(params, SongInfoParam::getHeadImgId, SongInfoParam::setHeadImg);
        return params;
    }
}
